﻿package plugin.instances
{
    import com.core.*;
    import com.sh.game.util.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import morn.core.handlers.*;
    import plugin.instances.game.*;
    import uiresource.instances.ui.*;

    public class InstancesGame extends InstancesUI
    {
        private var _curInstance:IInstance;
        private var showState:Boolean = false;
        private var _delayGroup:int = 0;
        private var _delayMsgDic:Dictionary;
        private var _type:int = 0;
        private var _lastTime:int = 0;
        private var _intvalid:int = 0;

        public function InstancesGame()
        {
            this._delayMsgDic = new Dictionary();
            var _loc_1:* = Control.create(InstancesControl, this);
            ModelProxy.addObserver("mainuiInstance", _loc_1);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.doStop);
            return;
        }// end function

        public function setShow(param1:Object) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Object = null;
            var _loc_2:* = param1.type;
            if (_loc_2 == 100)
            {
                label_title.text = "押送游城";
            }
            else if (_loc_2 == 102)
            {
                label_title.text = "行会押镖";
            }
            else
            {
                _loc_3 = ConfigDictionary.data.maplist;
                _loc_4 = _loc_3[param1.mapid];
                label_title.text = _loc_4.name;
            }
            if (this._type != _loc_2)
            {
                this.destroyShow();
            }
            this._type = _loc_2;
            if (!this._curInstance)
            {
                switch(_loc_2)
                {
                    case InstanceType.NORMAL:
                    {
                        this._curInstance = new FbviewGame();
                        img_bg.height = 320;
                        break;
                    }
                    case InstanceType.FANPAI:
                    {
                        this._curInstance = new FbkGame();
                        img_bg.height = 400;
                        break;
                    }
                    case InstanceType.TD:
                    {
                        this._curInstance = new SuperTDCopyGame();
                        box_layer.addChild(this._curInstance as DisplayObject);
                        this._delayGroup = 0;
                        this._curInstance.initShow(param1, this._delayMsgDic);
                        this._delayMsgDic = new Dictionary();
                        this._curInstance.initEvent();
                        img_bg.height = 390;
                        break;
                    }
                    case InstanceType.YAOQIANSHU:
                    {
                        this._curInstance = new YaoQianShuGame();
                        box_layer.addChild(this._curInstance as DisplayObject);
                        this._delayGroup = 0;
                        this._curInstance.initShow(param1, this._delayMsgDic);
                        this._delayMsgDic = new Dictionary();
                        this._curInstance.initEvent();
                        img_bg.height = 410;
                        break;
                    }
                    case InstanceType.TIANGUAN:
                    {
                        this._curInstance = new ChangCopyGame();
                        img_bg.height = 400;
                        break;
                    }
                    case InstanceType.EMS:
                    {
                        this._curInstance = new EmsGame();
                        img_bg.height = 420;
                        break;
                    }
                    case InstanceType.SHIWANGDIAN1:
                    {
                        this._curInstance = new HeShangDianGmae();
                        img_bg.height = 470;
                        break;
                    }
                    case InstanceType.SHIWANGDIAN2:
                    {
                        this._curInstance = new ShiWangDianGame();
                        img_bg.height = 405;
                        break;
                    }
                    case InstanceType.CHENGZHUZHIREN:
                    {
                        this._curInstance = new CZZRGame();
                        img_bg.height = 400;
                        break;
                    }
                    case InstanceType.SHENWEIYU:
                    {
                        this._curInstance = new SwyGame();
                        img_bg.height = 400;
                        break;
                    }
                    case InstanceType.YIWANZOULAN:
                    {
                        this._curInstance = new YwzlGame();
                        img_bg.height = 360;
                        break;
                    }
                    case InstanceType.YEXIFENGMOGU:
                    {
                        this._curInstance = new YxfmgGame();
                        img_bg.height = 400;
                        break;
                    }
                    case InstanceType.SBK:
                    {
                        this._curInstance = new SBKGame();
                        img_bg.height = 400;
                        this.SBKIintTime(0);
                        break;
                    }
                    case InstanceType.SHIMUMIZHEN:
                    {
                        this._curInstance = new SmmzGame();
                        img_bg.height = 400;
                        break;
                    }
                    case InstanceType.ZHUANSHENG:
                    {
                        this._curInstance = new FbkGame();
                        img_bg.height = 400;
                        break;
                    }
                    case InstanceType.YEHEFENGGAO:
                    {
                        this._curInstance = new YhfgGame();
                        img_bg.height = 460;
                        break;
                    }
                    case InstanceType.UNIONBOSS:
                    {
                        this._curInstance = new UnionBossGame();
                        img_bg.height = 380;
                        break;
                    }
                    case InstanceType.UNIONCOLLECTION:
                    {
                        this._curInstance = new UnionCjGame();
                        img_bg.height = 460;
                        break;
                    }
                    case InstanceType.UNIONEMS:
                    {
                        this._curInstance = new UnionEmsGame();
                        img_bg.height = 360;
                        break;
                    }
                    default:
                    {
                        return;
                        break;
                    }
                }
                box_layer.addChild(this._curInstance as DisplayObject);
            }
            btn_show.y = (img_bg.height - btn_show.height) / 2;
            btn_unshow.y = (img_bg.height - btn_unshow.height) / 2;
            this._curInstance.initShow(param1, this._delayMsgDic);
            this._curInstance.initEvent();
            this._delayMsgDic = new Dictionary();
            return;
        }// end function

        public function updateShow(param1:int, param2:int, param3:Object) : void
        {
            if (param2 == InstancesPublic.TIMECHECKER)
            {
                this.initTime(param3);
            }
            else if (this._curInstance)
            {
                if (param2 == InstancesPublic.TDEnd)
                {
                    this.clearTime();
                }
                else if (param2 == InstancesPublic.TIMESTOP)
                {
                    this.clearTime();
                }
                this._curInstance.updateShow(param2, param3);
            }
            else
            {
                this._delayGroup = param1;
                this._delayMsgDic[param2] = param3;
            }
            return;
        }// end function

        public function initEvent() : void
        {
            btn_show.clickHandler = new Handler(this.changeShow);
            btn_unshow.clickHandler = new Handler(this.changeShow);
            return;
        }// end function

        protected function doStop(event:MouseEvent) : void
        {
            event.stopPropagation();
            return;
        }// end function

        private function changeShow() : void
        {
            box_layer.visible = this.showState;
            this.showState = !this.showState;
            return;
        }// end function

        public function destroyShow() : void
        {
            if (this._curInstance)
            {
                this._curInstance.destroyShow(this._delayMsgDic);
                box_layer.removeChild(this._curInstance as DisplayObject);
                this._curInstance = null;
            }
            return;
        }// end function

        public function clearTime() : void
        {
            if (this._intvalid > 0)
            {
                SecondTimerUtil.instance.removeTask(this._intvalid);
            }
            this._intvalid = 0;
            return;
        }// end function

        public function initTime(param1:Object) : void
        {
            if (this._intvalid > 0)
            {
                SecondTimerUtil.instance.removeTask(this._intvalid);
            }
            var _loc_2:* = param1[0];
            var _loc_3:* = Number(param1[1]) / 1000;
            var _loc_4:* = _loc_2.totaltime;
            var _loc_5:* = _loc_2.ctime / 1000;
            this._lastTime = _loc_4 + _loc_5 - _loc_3;
            this._intvalid = SecondTimerUtil.instance.addSecondTask(this.timerHandler);
            ModelProxy.change("data", "tdtime", this._lastTime);
            return;
        }// end function

        public function SBKIintTime(param1:int) : void
        {
            if (this._intvalid > 0)
            {
                SecondTimerUtil.instance.removeTask(this._intvalid);
            }
            this._lastTime = param1;
            var _loc_2:* = ConfigDictionary.data.gameactive[13];
            var _loc_3:* = _loc_2.time;
            var _loc_4:* = _loc_3.split("-");
            var _loc_5:* = (_loc_3.split("-")[0] as String).split("#");
            var _loc_6:* = (_loc_4[1] as String).split("#");
            var _loc_7:* = ModelProxy.getValue("data", "servertime") + Number(getTimer());
            var _loc_8:* = new Date();
            new Date().setTime(_loc_7);
            var _loc_9:* = new Date(_loc_8.fullYear, _loc_8.month, _loc_8.date, int(_loc_5[0]), int(_loc_5[1]), 0, 0);
            var _loc_10:* = new Date(_loc_8.fullYear, _loc_8.month, _loc_8.date, int(_loc_6[0]), int(_loc_6[1]), 0, 0);
            this._lastTime = _loc_10.time / 1000 - _loc_7 / 1000;
            this._intvalid = SecondTimerUtil.instance.addSecondTask(this.timerHandler);
            return;
        }// end function

        private function timerHandler() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this._lastTime - 1;
            _loc_1._lastTime = _loc_2;
            if (this._lastTime > 0)
            {
                if (this._curInstance)
                {
                    this._curInstance.updateShow(InstancesPublic.TIMECHECKER, this._lastTime);
                }
            }
            else if (this._intvalid > 0)
            {
                SecondTimerUtil.instance.removeTask(this._intvalid);
            }
            return;
        }// end function

    }
}
